package com.airlines.crewassignment.service.crew.impl;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.crewassignment.constant.CrewExamineConstant;
import com.airlines.crewassignment.daofqm.CrewExamineMapper;
import com.airlines.crewassignment.domain.DateTimeRange;
import com.airlines.crewassignment.domain.crew.CrewExamine;
import com.airlines.crewassignment.service.crew.CrewExamineService;

/**
 * @author xxxx
 * @date 2018年5月9日
 * @description
 */
@Service
public class CrewExamineServiceImpl implements CrewExamineService {
	@Autowired(required = false)
	private CrewExamineMapper crewExamineMapper;

	@Override
	public List<CrewExamine> listCrewExamineALL(String workcode) {
		List<CrewExamine> listAll = new ArrayList<>();
		listAll.addAll(this.listCrewExamineSkill(workcode));
		listAll.addAll(this.listCrewExamineEmergency(workcode));
		listAll.addAll(this.listCrewExamineAirlineCheck(workcode));
		listAll.addAll(this.listCrewLicense(workcode));
		listAll.addAll(this.listCrewPhysicalExamination(workcode));
		listAll.addAll(this.listCrewBoarding(workcode));

		return listAll;
	}

	@Override
	public List<CrewExamine> listCrewExamineSkill(String workcode) {
		List<CrewExamine> list = crewExamineMapper.listCrewExamineSkill(workcode);
		this.initCrewExamineTypeList(list, CrewExamineConstant.SKILL, CrewExamineConstant.SKILL_NAME);
		return list;
	}

	@Override
	public List<CrewExamine> listCrewExamineEmergency(String workcode) {
		List<CrewExamine> list = crewExamineMapper.listCrewExamineEmergency(workcode);
		this.initCrewExamineTypeList(list, CrewExamineConstant.EMERGENCY, CrewExamineConstant.EMERGENCY_NAME);
		return list;
	}

	@Override
	public List<CrewExamine> listCrewExamineAirlineCheck(String workcode) {
		List<CrewExamine> list = crewExamineMapper.listCrewExamineAirlineCheck(workcode);
		this.initCrewExamineTypeList(list, CrewExamineConstant.AIRLINE_CHECK, CrewExamineConstant.AIRLINE_CHECK_NAME);
		return list;
	}

	@Override
	public List<CrewExamine> listCrewLicense(String workcode) {
		List<CrewExamine> list = crewExamineMapper.listCrewLicense(workcode);
		this.initCrewExamineTypeList(list, CrewExamineConstant.LICENSE, CrewExamineConstant.LICENSE_NAME);
		return list;
	}

	private void initCrewExamineTypeList(List<CrewExamine> list, String examineTypeCode, String examineTypeName) {
		DateTime startDefault = new DateTime(2000, 1, 1, 0, 0, 0);
		for (CrewExamine c : list) {
			c.setExamineTypeCode(examineTypeCode);
			c.setExamineTypeName(examineTypeName);
			if (c.getStart() == null) {
				c.setStart(startDefault.toDate());
			}
			DateTimeRange usableDateTimeRange = new DateTimeRange(c.getStart(), c.getEnd());
			c.setUsableDateTimeRange(usableDateTimeRange);
		}
	}

	@Override
	public List<CrewExamine> listCrewPhysicalExamination(String workcode) {
		List<CrewExamine> list = crewExamineMapper.listCrewPhysicalExamination(workcode);
		this.initCrewExamineTypeList(list, CrewExamineConstant.PHYSICAL_EXAMINATION,
				CrewExamineConstant.PHYSICAL_EXAMINATION_NAME);
		return list;
	}

	@Override
	public List<CrewExamine> listCrewBoarding(String workcode) {
		List<CrewExamine> list = crewExamineMapper.listCrewBoarding(workcode);
		this.initCrewExamineTypeList(list, CrewExamineConstant.BOARDING, CrewExamineConstant.BOARDING_NAME);
		return list;
	}

}
